/*
Copyright (C) 2012 Brady Vidovic

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ftr.tools;

import btv.logger.BTVLogLevel;
import java.io.File;
import btv.tools.BTVProps;
import java.util.Collection;
import java.util.LinkedHashSet;
import static ftr.tools.Constants.*;
import static btv.tools.BTVTools.*;

/**
 * Static param class. Loads configuration params from .properties file
 * @author bvidovic
 */
public class Config extends BTVProps
{
    
    public String FTR_WEBSERVICE_URL, XBMC_WEBSERVER_URL, SEASON_WORD, FFMPEG_EXE_PATH, EMAIL_FROM_ADDRESS, POST_PROCESSING_BAT_FILE;
    public int WAIT_SEC, XBMC_LIBRARY_UPDATE_SEC, WAIT_FOR_MORE_PARTS_SEC;
    public BTVLogLevel EMAIL_IF;
    public boolean ARTIFACT_DELETE, EDL_CONVERSION, XBMC_LIBRARY_UPDATE, UPDATE_ENTIRE_XBMC_LIBRARY, PROCESS_PARTIAL_RECORDINGS, JOIN_PARTIAL_RECORDINGS, DB_ADD_PARTIAL_EPISODES, EMAIL_NOTIFICATIONS;
    public LinkedHashSet<String> ARTIFACT_EXTS, VIDEO_EXTS, EXCLUDED_SERIESNAMES, EXCLUDED_GENRES, NOTIFICATION_RECIPIENTS;
    public String[] VIDEO_EXTS_ARRAY, ARTIFACT_EXTS_ARRAY;
    
      
    public Config(File propertiesFile)
    {                
        super(propertiesFile);
        
        FTR_WEBSERVICE_URL = getTrimmedString("FTR_WEBSERVICE_URL");
        if(!valid(FTR_WEBSERVICE_URL)) 
            Logger.ERROR("No FTR_WEBSERVICE_URL found in properties file!");
        
        FTR_WEBSERVICE_URL = trimTrailingSlashes(FTR_WEBSERVICE_URL);
        Logger.DEBUG("FTR_WEBSERVICE_URL = "+ FTR_WEBSERVICE_URL);
        
        WAIT_SEC=getInt("WAIT_SEC",0);        
        Logger.DEBUG("WAIT_SEC = "+ WAIT_SEC);
        
        ARTIFACT_DELETE = getBoolean("ARTIFACT_DELETE",false);
        Logger.DEBUG("ARTIFACT_DELETE = "+ ARTIFACT_DELETE);
        
        ARTIFACT_EXTS = getPipeDelimitedLowercaseTrimmedSet("ARTIFACT_EXTS");//lowercase,trimmed values
        ARTIFACT_EXTS_ARRAY = toLcUcArray(ARTIFACT_EXTS);//convert to string[] for use with FileUtils 
        Logger.DEBUG("ARTIFACT_EXTS = "+ ARTIFACT_EXTS);        
        
        VIDEO_EXTS = getPipeDelimitedLowercaseTrimmedSet("VIDEO_EXTS");
        VIDEO_EXTS_ARRAY = toLcUcArray(VIDEO_EXTS);//convert to string[] for use with FileUtils 
        Logger.DEBUG("VIDEO_EXTS = "+ VIDEO_EXTS);                
        
        SEASON_WORD = getTrimmedString("SEASON_WORD");
        if(!valid(SEASON_WORD)) SEASON_WORD = "Season";
        Logger.DEBUG("SEASON_WORD = "+ SEASON_WORD);
                
        EDL_CONVERSION = getBoolean("EDL_CONVERSION",false);
        Logger.DEBUG("EDL_CONVERSION = "+ EDL_CONVERSION);       
        
        XBMC_LIBRARY_UPDATE = getBoolean("XBMC_LIBRARY_UPDATE",false);
        Logger.DEBUG("XBMC_LIBRARY_UPDATE = "+ XBMC_LIBRARY_UPDATE);
        
        XBMC_LIBRARY_UPDATE_SEC = getInt("XBMC_LIBRARY_UPDATE_SEC", 60);
        Logger.DEBUG("XBMC_LIBRARY_UPDATE_SEC = "+ XBMC_LIBRARY_UPDATE_SEC);
        
        UPDATE_ENTIRE_XBMC_LIBRARY = getBoolean("UPDATE_ENTIRE_XBMC_LIBRARY", false);
        Logger.DEBUG("UPDATE_ENTIRE_XBMC_LIBRARY = "+ UPDATE_ENTIRE_XBMC_LIBRARY);
        
        XBMC_WEBSERVER_URL = trimTrailingSlashes(getTrimmedString("XBMC_WEBSERVER_URL"));
        Logger.DEBUG("XBMC_WEBSERVER_URL = "+ XBMC_WEBSERVER_URL);
        if(XBMC_LIBRARY_UPDATE && !valid(XBMC_WEBSERVER_URL)) 
            Logger.ERROR("XBMC_LIBRARY_UPDATE=true, but no XBMC_WEBSERVER_URL found!");
        
        EXCLUDED_SERIESNAMES = getPipeDelimitedLowercaseTrimmedSet("EXCLUDED_SERIESNAMES");
        Logger.DEBUG("EXCLUDED_SERIESNAMES = "+ EXCLUDED_SERIESNAMES);
        
        EXCLUDED_GENRES = getPipeDelimitedLowercaseTrimmedSet("EXCLUDED_GENRES");
        Logger.DEBUG("EXCLUDED_GENRES = "+ EXCLUDED_GENRES);               
        
        PROCESS_PARTIAL_RECORDINGS = getBoolean("PROCESS_PARTIAL_RECORDINGS", false);
        Logger.DEBUG("PROCESS_PARTIAL_RECORDINGS = "+ PROCESS_PARTIAL_RECORDINGS);               
        
        POST_PROCESSING_BAT_FILE = getString("POST_PROCESSING_BAT_FILE");
        Logger.DEBUG("POST_PROCESSING_BAT_FILE = "+ POST_PROCESSING_BAT_FILE);
        
        WAIT_FOR_MORE_PARTS_SEC = getInt("WAIT_FOR_MORE_PARTS_SEC", 60 * 5);
        Logger.DEBUG("WAIT_FOR_MORE_PARTS_SEC = "+ WAIT_FOR_MORE_PARTS_SEC);                       
        
        JOIN_PARTIAL_RECORDINGS = getBoolean("JOIN_PARTIAL_RECORDINGS", false);
        Logger.DEBUG("JOIN_PARTIAL_RECORDINGS = "+ JOIN_PARTIAL_RECORDINGS);               
        if(JOIN_PARTIAL_RECORDINGS && !PROCESS_PARTIAL_RECORDINGS)
        {
            Logger.WARN("JOIN_PARTIAL_RECORDINGS is "+JOIN_PARTIAL_RECORDINGS+", but PROCESS_PARTIAL_RECORDINGS is "+PROCESS_PARTIAL_RECORDINGS+". "
                    + "Will not process any partial recordings unless PROCESS_PARTIAL_RECORDINGS is true");
            JOIN_PARTIAL_RECORDINGS = false;
        }
        
        FFMPEG_EXE_PATH = getString("FFMPEG_EXE_PATH");
        Logger.DEBUG("FFMPEG_EXE_PATH = "+ FFMPEG_EXE_PATH);               
        if(JOIN_PARTIAL_RECORDINGS && !valid(FFMPEG_EXE_PATH))
        {
            Logger.WARN("JOIN_PARTIAL_RECORDINGS is "+ JOIN_PARTIAL_RECORDINGS+", but no FFMPEG_EXE_PATH found. Cannot JOIN_PARTIAL_RECORDINGS...");
            JOIN_PARTIAL_RECORDINGS = false;
        }
                
        DB_ADD_PARTIAL_EPISODES = getBoolean("DB_ADD_PARTIAL_EPISODES", false);
        Logger.DEBUG("DB_ADD_PARTIAL_EPISODES = "+ DB_ADD_PARTIAL_EPISODES);               
        if(DB_ADD_PARTIAL_EPISODES && !PROCESS_PARTIAL_RECORDINGS)
        {
            Logger.WARN("DB_ADD_PARTIAL_EPISODES is "+DB_ADD_PARTIAL_EPISODES+", but PROCESS_PARTIAL_RECORDINGS is "+PROCESS_PARTIAL_RECORDINGS+". "
                    + "Will not process any partial recordings unless PROCESS_PARTIAL_RECORDINGS is true");
            DB_ADD_PARTIAL_EPISODES = false;
        }
                
        EMAIL_NOTIFICATIONS = getBoolean("EMAIL_NOTIFICATIONS", false);
        Logger.DEBUG("EMAIL_NOTIFICATIONS = "+ EMAIL_NOTIFICATIONS);        
        if(EMAIL_NOTIFICATIONS)
        {
            String strEMAIL_IF = getString("EMAIL_IF", BTVLogLevel.ERROR.toString()).trim().toUpperCase();
            if("WARNING".equals(strEMAIL_IF)) strEMAIL_IF="WARN";//helper conversion                
            try
            {
                EMAIL_IF = BTVLogLevel.valueOf(strEMAIL_IF);
            }
            catch(IllegalArgumentException unknownLevel)
            {
                EMAIL_IF = BTVLogLevel.ERROR;
                Logger.WARN("EMAIL_IF level \""+strEMAIL_IF+"\" is unknown. Will use default of " + EMAIL_IF);
            }
            Logger.DEBUG("EMAIL_IF = "+ EMAIL_IF);        
            NOTIFICATION_RECIPIENTS = getSet("NOTIFICATION_RECIPIENTS", "|", false, true);
            EMAIL_FROM_ADDRESS = getString("FROM_ADDRESS", "error_notification-FTR-XBMC");            
        }
    }
    

    
    private static String[] toLcUcArray(Collection<String> collection)
    {
        LinkedHashSet<String> results = new LinkedHashSet<String> ();
        for(String s : collection)
        {
            results.add(s);//possible mixed case
            results.add(s.toLowerCase());
            results.add(s.toUpperCase());
        }
        
        //convert to string[]
        String[] arr = new String[results.size()];
        int i=0;
        for(String s : results)
            arr[i++]=s;        
        return arr;
    }        
}